import 'dart:collection';
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/style.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:shop/shop/merchant/GoodsInfoEdit.dart';
import 'package:shop/config/config.dart';
import 'package:shop/http/HttpUtil.dart';
import 'package:shop/route/my_route.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
class GoodsGuige extends StatefulWidget {
  List guiges = [];//商品规格集合
  var tu1;var tu2;var tu3;var tu4;var tu5;//商品图片
  var tu6;var tu7;var tu8;//商品详情图片
  String gName;String gPrice;String danwei;String kucun;String miaoshu;String goodsTypeName;//商品信息
  int type;//类型  1：新增  2：修改
  String id;//商品ID
  String fuwuPicture;   //服务图片json
  String detailPicture; //详情图片json
  GoodsGuige({Key key, this.title,this.kucun,this.gPrice,this.goodsTypeName,this.miaoshu,this.gName,this.danwei,this.tu1,this.tu2,this.tu3,this.tu4,this.tu5,this.tu6,this.tu7,this.tu8,this.guiges,this.type,this.id,this.fuwuPicture,this.detailPicture}) : super(key: key);

  final String title;

  @override
  _GoodsGuige createState() => _GoodsGuige(kucun,gPrice,goodsTypeName,miaoshu,gName,danwei,tu1,tu2,tu3,tu4,tu5,tu6,tu7,tu8,guiges,type,id,fuwuPicture,detailPicture);
}

class _GoodsGuige extends State<GoodsGuige> {

  TextEditingController _name1 = new TextEditingController();
  TextEditingController _name2 = new TextEditingController();
  TextEditingController _name3 = new TextEditingController();
  TextEditingController _name4 = new TextEditingController();
  TextEditingController _name5 = new TextEditingController();
  TextEditingController _value1 = new TextEditingController();
  TextEditingController _value2 = new TextEditingController();
  TextEditingController _value3 = new TextEditingController();
  TextEditingController _value4 = new TextEditingController();
  TextEditingController _value5 = new TextEditingController();
  var name1;//规格名称1
  var name2;var name3;var name4;var name5;
  var value1;//规格值1
  var value2;var value3;var value4;var value5;
  int groupNum=1;//规格数量
  List guiges = [];//商品规格

  var tu1;var tu2;var tu3;var tu4;var tu5;//商品图片
  var tu6;var tu7;var tu8;//商品详情图片
  String gName;String gPrice;String danwei;String kucun;String miaoshu;String goodsTypeName;//商品信息
  int type;//类型  1：新增  2：修改
  String id;//商品ID
  String fuwuPicture;   //服务图片json
  String detailPicture; //详情图片json
  //初始化数据
  _GoodsGuige(kucun,gPrice,goodsTypeName,miaoshu,gName,danwei,tu1,tu2,tu3,tu4,tu5,tu6,tu7,tu8,guiges,type,id,fuwuPicture,detailPicture){
    this.danwei=danwei;this.gName=gName;this.miaoshu=miaoshu;this.goodsTypeName=goodsTypeName;this.kucun=kucun;
    this.gPrice=gPrice;this.tu1=tu1;this.tu2=tu2;this.tu3=tu3;this.tu4=tu4;this.tu5=tu5;this.guiges=guiges;
    this.tu6=tu6;this.tu7=tu7;this.tu8=tu8;
    this.type=type;this.id=id;
    this.fuwuPicture=fuwuPicture;this.detailPicture=detailPicture;
    int changdu = guiges.length;
    if(changdu>0){
      groupNum = changdu;
      if(changdu==1){
        _name1 = new TextEditingController(text: guiges[0]['name']);_value1 = new TextEditingController(text: guiges[0]['value']);
      }else if(changdu==2){
        _name1 = new TextEditingController(text: guiges[0]['name']);_value1 = new TextEditingController(text: guiges[0]['value']);
        _name2 = new TextEditingController(text: guiges[1]['name']);_value2 = new TextEditingController(text: guiges[1]['value']);
      }else if(changdu==3){
        _name1 = new TextEditingController(text: guiges[0]['name']);_value1 = new TextEditingController(text: guiges[0]['value']);
        _name2 = new TextEditingController(text: guiges[1]['name']);_value2 = new TextEditingController(text: guiges[1]['value']);
        _name3 = new TextEditingController(text: guiges[2]['name']);_value3 = new TextEditingController(text: guiges[2]['value']);
      }else if(changdu==4){
        _name1 = new TextEditingController(text: guiges[0]['name']);_value1 = new TextEditingController(text: guiges[0]['value']);
        _name2 = new TextEditingController(text: guiges[1]['name']);_value2 = new TextEditingController(text: guiges[1]['value']);
        _name3 = new TextEditingController(text: guiges[2]['name']);_value3 = new TextEditingController(text: guiges[2]['value']);
        _name4 = new TextEditingController(text: guiges[3]['name']);_value4 = new TextEditingController(text: guiges[3]['value']);
      }else{
        _name1 = new TextEditingController(text: guiges[0]['name']);_value1 = new TextEditingController(text: guiges[0]['value']);
        _name2 = new TextEditingController(text: guiges[1]['name']);_value2 = new TextEditingController(text: guiges[1]['value']);
        _name3 = new TextEditingController(text: guiges[2]['name']);_value3 = new TextEditingController(text: guiges[2]['value']);
        _name4 = new TextEditingController(text: guiges[3]['name']);_value4 = new TextEditingController(text: guiges[3]['value']);
        _name5 = new TextEditingController(text: guiges[4]['name']);_value5 = new TextEditingController(text: guiges[4]['value']);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context , width: 464 , height: 819) ;
    return Scaffold(
      appBar: AppBar(
        title: Text("商品规格"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            // 第一组
            Column(
              children: [
                Text("第一组"),
                Row(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Text("规格名称:",style: TextStyle(color: Colors.black,fontSize: 19),),
                        // Text("如:尺寸",style: TextStyle(color: Colors.black38),)
                      ],
                    ),
                    SizedBox(width: 10,),
                    Expanded(child: TextFormField(
                      controller: _name1,
                      decoration: InputDecoration(hintText: "如：尺寸")
                    ))
                  ],
                ),
                Row(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Text("规格值:",style: TextStyle(color: Colors.black,fontSize: 19),),
                        // Text("如:大-中-小",style: TextStyle(color: Colors.black38),)
                      ],
                    ),
                    SizedBox(width: 30,),
                    Expanded(child: TextFormField(
                      controller: _value1,
                      decoration: InputDecoration(hintText: "如：大-中-小")
                    ))
                  ],
                ),
                Divider(),
                SizedBox(height: 10,),
              ],
            ),
          // 第二组
          groupNum>1?  Column(
              children: [
                Text("第二组"),
                Row(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Text("规格名称:",style: TextStyle(color: Colors.black,fontSize: 19),),
                        // Text("如:颜色",style: TextStyle(color: Colors.black38),)
                      ],
                    ),
                    SizedBox(width: 10,),
                    Expanded(child: TextFormField(
                      controller: _name2,
                      decoration: InputDecoration(hintText: "如:颜色")
                    ))
                  ],
                ),
                Row(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Text("规格值:",style: TextStyle(color: Colors.black,fontSize: 19),),
                        // Text("如:红色-蓝色-黄色",style: TextStyle(color: Colors.black38),)
                      ],
                    ),
                    SizedBox(width: 30,),
                    Expanded(child: TextFormField(
                      controller: _value2,
                      decoration: InputDecoration(hintText: "如:红色-蓝色-黄色")
                    )
                    )
                  ],
                ),
                Divider(),
                SizedBox(height: 10,),
              ],
            ) : Text(""),

           // 第三组
           groupNum>2? Column(
              children: [
                Text("第三组"),
                Row(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Text("规格名称:",style: TextStyle(color: Colors.black,fontSize: 19),),
                        // Text("如:颜色",style: TextStyle(color: Colors.black38),)
                      ],
                    ),
                    SizedBox(width: 10,),
                    Expanded(child: TextFormField(
                      controller: _name3,
                      decoration: InputDecoration(hintText: "如:颜色")
                    ))
                  ],
                ),
                Row(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Text("规格值:",style: TextStyle(color: Colors.black,fontSize: 19),),
                        // Text("如:红色-蓝色-黄色",style: TextStyle(color: Colors.black38),)
                      ],
                    ),
                    SizedBox(width: 30,),
                    Expanded(child: TextFormField(
                      controller: _value3,
                      decoration: InputDecoration(hintText: "如:红色-蓝色-黄色")
                    ))
                  ],
                ),
                Divider(),
                SizedBox(height: 10,),
              ],
            ) :Text(""),

            //第四组
            groupNum>3? Column(
              children: [
                Text("第四组"),
                Row(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Text("规格名称:",style: TextStyle(color: Colors.black,fontSize: 19),),
                        // Text("如:颜色",style: TextStyle(color: Colors.black38),)
                      ],
                    ),
                    SizedBox(width: 10,),
                    Expanded(child: TextFormField(
                      controller: _name4,
                      decoration: InputDecoration(hintText: "如:颜色")
                    ))
                  ],
                ),
                Row(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Text("规格值:",style: TextStyle(color: Colors.black,fontSize: 19),),
                        // Text("如:红色-蓝色-黄色",style: TextStyle(color: Colors.black38),)
                      ],
                    ),
                    SizedBox(width: 30,),
                    Expanded(child: TextFormField(
                      controller: _value4,
                      decoration: InputDecoration(hintText: "如:红色-蓝色-黄色")
                    ))
                  ],
                ),
                Divider(),
                SizedBox(height: 10,),
              ],
            ) :Text(""),

            groupNum>4? Column(
              children: [
                Text("第五组"),
                Row(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Text("规格名称:",style: TextStyle(color: Colors.black,fontSize: 19),),
                        // Text("如:颜色",style: TextStyle(color: Colors.black38),)
                      ],
                    ),
                    SizedBox(width: 10,),
                    Expanded(child: TextFormField(
                      controller: _name5,
                      decoration: InputDecoration(hintText: "如:颜色")
                    ))
                  ],
                ),
                Row(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Text("规格值:",style: TextStyle(color: Colors.black,fontSize: 19),),
                        // Text("如:红色-蓝色-黄色",style: TextStyle(color: Colors.black38),)
                      ],
                    ),
                    SizedBox(width: 30,),
                    Expanded(child: TextFormField(
                      controller: _value5,
                      decoration: InputDecoration(hintText: "如:红色-蓝色-黄色")
                    ))
                  ],
                ),
                Divider(),
                SizedBox(height: 10,),
              ],
            ) :Text(""),
            GestureDetector(
              child: Container(
                margin: EdgeInsets.symmetric(vertical: 10),
                padding: EdgeInsets.symmetric(vertical: 10),
                color: Colors.red,
                child: Center(
                  child: Text("保存"),
                ),
              ),onTap: (){
                var map1 = new HashMap();var map2 = new HashMap();var map3 = new HashMap();var map4 = new HashMap();var map5 = new HashMap();
                guiges = [];
                name1 = _name1.text.trim();value1 = _value1.text.trim();
                name2 = _name2.text.trim();value2 = _value2.text.trim();
                name3 = _name3.text.trim();value3 = _value3.text.trim();
                name4 = _name4.text.trim();value4 = _value4.text.trim();
                name5 = _name5.text.trim();value5 = _value5.text.trim();
                if(groupNum==1){
                  if(name1.length<1 || value1.length<1){Fluttertoast.showToast(msg: "请将规格填写完整");return;}
                  map1['name'] = name1;map1['value'] = value1;guiges.add(map1);
                }else if(groupNum==2){
                  if(name1.length<1 || value1.length<1){Fluttertoast.showToast(msg: "请将第一组规格填写完整");return;}
                  if(name2.length<1 || value2.length<1){Fluttertoast.showToast(msg: "请将第二组规格填写完整");return;}
                  map1['name'] = name1;map1['value'] = value1;guiges.add(map1);
                  map2['name'] = name2;map2['value'] = value2;guiges.add(map2);
                }else if(groupNum==3){
                  if(name1.length<1 || value1.length<1){Fluttertoast.showToast(msg: "请将第一组规格填写完整");return;}
                  if(name2.length<1 || value2.length<1){Fluttertoast.showToast(msg: "请将第二组规格填写完整");return;}
                  if(name3.length<1 || value3.length<1){Fluttertoast.showToast(msg: "请将第三组规格填写完整");return;}
                  map1['name'] = name1;map1['value'] = value1;guiges.add(map1);
                  map2['name'] = name2;map2['value'] = value2;guiges.add(map2);
                  map3['name'] = name3;map3['value'] = value3;guiges.add(map3);
                }else if(groupNum==4){
                  if(name1.length<1 || value1.length<1){Fluttertoast.showToast(msg: "请将第一组规格填写完整");return;}
                  if(name2.length<1 || value2.length<1){Fluttertoast.showToast(msg: "请将第二组规格填写完整");return;}
                  if(name3.length<1 || value3.length<1){Fluttertoast.showToast(msg: "请将第三组规格填写完整");return;}
                  if(name4.length<1 || value4.length<1){Fluttertoast.showToast(msg: "请将第四组规格填写完整");return;}
                  map1['name'] = name1;map1['value'] = value1;guiges.add(map1);
                  map2['name'] = name2;map2['value'] = value2;guiges.add(map2);
                  map3['name'] = name3;map3['value'] = value3;guiges.add(map3);
                  map4['name'] = name4;map4['value'] = value4;guiges.add(map4);
                }else{
                  if(name1.length<1 || value1.length<1){Fluttertoast.showToast(msg: "请将第一组规格填写完整");return;}
                  if(name2.length<1 || value2.length<1){Fluttertoast.showToast(msg: "请将第二组规格填写完整");return;}
                  if(name3.length<1 || value3.length<1){Fluttertoast.showToast(msg: "请将第三组规格填写完整");return;}
                  if(name4.length<1 || value4.length<1){Fluttertoast.showToast(msg: "请将第四组规格填写完整");return;}
                  if(name5.length<1 || value5.length<1){Fluttertoast.showToast(msg: "请将第五组规格填写完整");return;}
                  map1['name'] = name1;map1['value'] = value1;guiges.add(map1);
                  map2['name'] = name2;map2['value'] = value2;guiges.add(map2);
                  map3['name'] = name3;map3['value'] = value3;guiges.add(map3);
                  map4['name'] = name4;map4['value'] = value4;guiges.add(map4);
                  map5['name'] = name5;map5['value'] = value5;guiges.add(map5);
                }
                // print("规格集合："+guiges.toString());
                // MyRoute.push(GoodsInfoEdit(guiges: guiges,gName: gName,gPrice: gPrice,kucun: kucun,danwei: danwei,miaoshu: miaoshu,goodsTypeName: goodsTypeName,tu1: tu1,tu2: tu2,tu3: tu3,tu4: tu4,tu5: tu5,tu6: tu6,tu7: tu7,tu8: tu8,type: type,id: id,fuwuPicture: fuwuPicture,detailPicture: detailPicture,));
                MyRoute.pop(guiges);
            },)
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: (){
          setState(() {
            if(groupNum<5)
              groupNum++;
          });
        },
      ),
    );
  }
}